package com.siyu.tickets.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.siyu.common.page.Pagination;
import com.siyu.common.page.SimplePage;
import com.siyu.common.web.CookieUtils;
import com.siyu.tickets.entity.AdminUser;
import com.siyu.tickets.manager.AdminUserMng;

/**
 * 操作员相关
 * 
 * @since 2013-7-21
 * @version 0.1
 * @author cuigx
 */
@Controller
public class AdminUserAct {

	private static final Logger log = LoggerFactory.getLogger(AdminUserAct.class);

	@RequestMapping("/user/list")
	public String list(String name, Boolean enable, Integer pageNo, HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(name, enable, SimplePage.cpn(pageNo), CookieUtils.getPageSize(request));
		model.addAttribute("name", name);
		model.addAttribute("enable", enable);
		model.addAttribute("pagination", pagination);
		model.addAttribute("pageNo", pagination.getPageNo());
		return "user/list";
	}

	@RequestMapping("/user/add")
	public String add(HttpServletRequest request, ModelMap model) {
		return "user/add";
	}

	@RequestMapping("/user/edit")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		AdminUser user = manager.findById(id);
		model.addAttribute("admin", user);
		return "user/edit";
	}

	@RequestMapping("/user/save")
	public String save(AdminUser bean, HttpServletRequest request, ModelMap model) {
		bean = manager.registAdminUser(bean);
		return "redirect:list";
	}

	@RequestMapping("/user/update")
	public String update(AdminUser bean, HttpServletRequest request, ModelMap model) {
		manager.updateAdminUser(bean);
		return "redirect:list";
	}

	@RequestMapping("/user/delete")
	public String delete(Integer[] ids, Integer pageNo, HttpServletRequest request, ModelMap model) {
		AdminUser[] beans = manager.deleteByIds(ids);
		for (AdminUser bean : beans) {
			log.info("delete AdminUser id={}", bean.getId());
		}
		return "redirect:list";
	}

	@RequestMapping("/user/enable")
	public String enable(Integer[] ids, Boolean enable, Integer pageNo, HttpServletRequest request, ModelMap model) {
		manager.enableByIds(ids, enable);
		return "redirect:list";
	}

	@Autowired
	private AdminUserMng manager;

}
